package com.sample.springbatch.job.sample.reader;

import com.sample.springbatch.job.sample.service.FixedByteLengthTokenizer;
import com.sample.springbatch.model.FieldJavaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.LineCallbackHandler;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FixedLengthTokenizer;
import org.springframework.batch.item.file.transform.Range;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.beans.PropertyEditor;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Flat文件格式读取类  固定字节长度 文件不同的编码格式不同
 * Created by zwg on 2017/8/8.
 */
@Component
public class FixedByteLengthFlatFileItemReader<T> extends FlatFileItemReader {

    Logger logger = LoggerFactory.getLogger(FixedByteLengthFlatFileItemReader.class);

    private static LinkedHashMap<String,Range> fieldMap = new LinkedHashMap();

    {
        fieldMap.put("id",          new Range(1,4));
        fieldMap.put("name",          new Range(5,24));
        fieldMap.put("age",          new Range(25,26));
        fieldMap.put("birthday",          new Range(27,36));
    }




    public FixedByteLengthFlatFileItemReader() {
        setResource(new ClassPathResource("jobdata/fixed_byte_length.dat"));
        setEncoding("GBK");
        setLineMapper(new DefaultLineMapper<FieldJavaType>() {{
            setEncoding("GBK");
            //setLineTokenizer(new FixedLengthTokenizer() {{  //一行内容分割默认用','分割
            //按编码格式转成字节，按字节长度分割，再转成对应的字符串
            setLineTokenizer(new FixedByteLengthTokenizer() {{
                setCharSet("GBK");
                /**设置属性与定长范围*/
                setEncoding("GBK");
                setNames(fieldMap.keySet().toArray(new String[fieldMap.size()]));
                setColumns(fieldMap.values().toArray(new Range[fieldMap.size()]));
                //setNames(new String[] { "id", "name","age","birthday"});
                //setColumns(new Range[] {new Range(1,4),new Range(5,24),new Range(25,26),new Range(27,36)});

            }});

            setFieldSetMapper(new BeanWrapperFieldSetMapper<FieldJavaType>() {{
                setTargetType(FieldJavaType.class);
                //定义特殊的类型转换
                Map<String, PropertyEditor> customerEditorMap = new HashMap<String, PropertyEditor>();
                customerEditorMap.put("java.util.Date", new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
                setCustomEditors(customerEditorMap);
            }});
            setSkippedLinesCallback(new LineCallbackHandler() {
                @Override
                public void handleLine(String s) {
                    logger.error("skip line:"+s);
                }
            });

        }});
    }


}
